package environment
{
    import away3d.materials.DefaultMaterialBase;
    
    import flash.display.BitmapData;
    import flash.display.GradientType;
    import flash.display.Shape;
    import flash.filters.BlurFilter;
    import flash.geom.Matrix;
    import flash.geom.Point;
    
    import objects.BitmapPlane;

    public class EnvironmentLayer
    {
        // The bitmap data this layer was originally built with... if available.
        public var envPlane : EnvironmentPlane;
        
        public var bitmapData :BitmapData;
        
        // r,g,b?
        public var bitmapBitshift : int;
        
        public var env :Environment;
        
        protected var basePlaneColor : uint;
        
        public var data : Array;
        
        public var material : DefaultMaterialBase;
        
        protected var heightMapFunction : Function;
        
        public var layerIx : int = -1;
        
        public function EnvironmentLayer(layerIx : int, env : Environment, material : DefaultMaterialBase, bitmapBitshift : int, basePlaneColor : uint, heightMapFunction : Function)
        {
            this.env = env;
            this.layerIx = layerIx;
            this.basePlaneColor = basePlaneColor;
            this.bitmapBitshift = bitmapBitshift;
            this.material = material;
            this.heightMapFunction = heightMapFunction;
        }
        
        
        public function invalidate() : void
        {
            envPlane.invalidateVertexBuffer();
            
        }
        public function generateRandom(cellNum : int, minRadius : Number, maxRadius : Number, minPower : Number, maxPower : Number, blur : Number, blurQuality : int) : void
        {
            bitmapData = new BitmapData(env.width, env.height, true, basePlaneColor);
            
            // First we generate toxic cells.
            var cells : Array = new Array(cellNum);
            
            for (var i : int = 0; i < cellNum * 2; i++)
            {
                cells[i++] = {pow: minPower + (Math.random() * (maxPower - minPower)), x: Math.random() * env.width / 2, y: Math.random() * env.height, w: minRadius + Math.random() * (maxRadius - minRadius), h: minRadius + Math.random() * (maxRadius - minRadius)};
                cells[i] = {pow: cells[i-1].pow, x: env.width - cells[i-1].x, y: env.height - cells[i-1].y, w: cells[i-1].w, h: cells[i-1].h};
            }
            
            for (i = 0; i < cellNum *2; i++)
            {
                var tcShape : Shape = new Shape();
                var mat : Matrix = new Matrix();
                
                mat.createGradientBox(cells[i].w, cells[i].h, 0, 0, 0);
                
                tcShape.graphics.beginGradientFill(GradientType.RADIAL, [uint(255 * cells[i].pow) << bitmapBitshift, basePlaneColor], [255, 0], [0, 0xFF], mat);
                tcShape.graphics.drawEllipse(0, 0, cells[i].w, cells[i].h);
                tcShape.graphics.endFill();
                
                var mat2 : Matrix = new Matrix();
                mat2.tx = cells[i].x - cells[i].w / 2;
                mat2.ty = cells[i].y - cells[i].h / 2;
                
                bitmapData.draw(tcShape, mat2);
            }
            
            var blurFilter : BlurFilter = new BlurFilter(blur, blur, blurQuality);
            bitmapData.applyFilter(bitmapData, bitmapData.rect, new Point(0, 0), blurFilter);
            
            envPlane = new EnvironmentPlane(material, env, this, heightMapFunction);
            
            env.parentContainer.addChild(envPlane);
        }
    }
}